package com.netease.financial.base.register;

import com.netease.financial.base.login.LoginView;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.domain.interactor.DefaultSubscriber;
import com.netease.financial.domain.interactor.UseCase;
import com.netease.financial.domain.model.PageInfo;
import com.netease.financial.mapper.NfsViewModelDataMapper;

import javax.inject.Named;

/**
 * Created by hzwuyufeng on 3/8/16.
 */
public class RegisterPresenterImpl implements RegisterPresenter {

    private static final String TAG = RegisterPresenterImpl.class.getSimpleName();

    private final UseCase pageInfoUseCase;
    private final UseCase registerUseCase;
    private final NfsViewModelDataMapper nfsViewModelDataMapper;

    private RegisterView registerView;

    public RegisterPresenterImpl(@Named("page_info") UseCase pageInfoUseCase, @Named("register") UseCase registerUseCase,
                                 NfsViewModelDataMapper nfsViewModelDataMapper) {
        this.pageInfoUseCase = pageInfoUseCase;
        this.registerUseCase = registerUseCase;
        this.nfsViewModelDataMapper = nfsViewModelDataMapper;
    }

    @Override
    public void setView(LoginView view) {

    }

    @Override
    public void register() {

    }

    @Override
    public void refreshPageInfo() {
        this.pageInfoUseCase.execute(new PageInfoSubscriber());
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        pageInfoUseCase.unsubscribe();
        registerUseCase.unsubscribe();
    }

    @Override
    public void initialize() {

    }

    private void handleGetPageInfoSuccessEvent(PageInfo model) {
        // TODO: refresh
        NfsLog.d(TAG, "PageInfoModel:" + model.toString());
        registerView.onSuccess(nfsViewModelDataMapper.transform(model, RegisterPageViewModel.class));
    }

    private final class PageInfoSubscriber extends DefaultSubscriber<PageInfo> {
        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            NfsLog.d(TAG, e.getMessage());
        }

        @Override
        public void onNext(PageInfo pageInfo) {
            super.onNext(pageInfo);
            RegisterPresenterImpl.this.handleGetPageInfoSuccessEvent(pageInfo);
        }
    }
}
